package cc.eddic.practice.concurrency.impl;
import cc.eddic.practice.concurrency.LogItem;
import cc.eddic.practice.concurrency.LogParser;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
@Slf4j
public class LogParserWithLoopImpl implements LogParser {


//        Map<String, List<LogItem>> resultMap = new HashMap<>();
//        File folder = new File(folderName);
//
//        if (folder.isDirectory()) {
//            File[] files = folder.listFiles((dir, name) -> name.endsWith(".log"));
//
//            if (files != null) {
//                LogItemParserImpl logItemParser = new LogItemParserImpl();
//
//                for (File file : files) {
//                    String filePath = file.getAbsolutePath();
//                    List<LogItem> logItems = new ArrayList<>();
//
//                    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
//                        String line;
//                        while ((line = reader.readLine()) != null) {
//                            Optional<LogItem> optionalLogItem = logItemParser.parse(line);
//                            optionalLogItem.ifPresent(logItems::add);
//                        }
//                    } catch (IOException e) {
//                        e.printStackTrace(); // Handle the exception properly in a real application
//                    }
//
//                    resultMap.put(filePath, logItems);
//                }
//            }
//        }
//
//        return resultMap;
//    }
    @Override
    public Map<String, List<LogItem>> parse(String folderName) {

    Map<String, List<LogItem>> resultMap = new HashMap<>();
    processFolder(new File(folderName), resultMap);
    return resultMap;
    }

    private void processFolder(File folder, Map<String, List<LogItem>> resultMap) {
        if (folder.isDirectory()) {
            File[] files = folder.listFiles((dir, name) -> name.endsWith(".log"));

            if (files != null) {
                LogItemParserImpl logItemParser = new LogItemParserImpl();

                for (File file : files) {
                    String filePath = file.getAbsolutePath();
                    List<LogItem> logItems = new ArrayList<>();

                    try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
                        String line;
                        while ((line = reader.readLine()) != null) {
                            Optional<LogItem> optionalLogItem = logItemParser.parse(line);
                            optionalLogItem.ifPresent(logItems::add);
                        }
                    } catch (IOException e) {
                        e.printStackTrace(); // Handle the exception properly in a real application
                    }

                    resultMap.put(filePath, logItems);
                }
            }

            File[] subFolders = folder.listFiles(File::isDirectory);
            if (subFolders != null) {
                for (File subfolder : subFolders) {
                    processFolder(subfolder, resultMap);
                }
            }
        }
    }
}




